package com.crm.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.internal.build.AllowSysOut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.jdao.IUserDAO;
import com.crm.model.mdao.RoleandusersMapper;
import com.crm.model.mdao.UserMapper;
import com.crm.model.mpojo.Roleandusers;
import com.crm.model.mpojo.SysDept;
import com.crm.model.mpojo.Users;
import com.crm.model.pojo.User;
import com.crm.services.RoleServices;
import com.crm.services.SysDeptServices;
import com.crm.services.SysTreeService;
import com.crm.services.UserServices;
import com.crm.util.DeptLevelDto;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageInfo;

import net.sf.json.processors.JsonBeanProcessorMatcher;

@Controller
public class UserController {
	
	@Autowired
	RoleandusersMapper roleandusersMapper;
	@Autowired
	SysTreeService sysTreeService;
	@Autowired
	UserServices userServices;
	@Autowired
	SysDeptServices deptServices;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	IUserDAO userDAO;
	
	@Autowired
	RoleServices roleServices;
	
	
	@RequestMapping("/userManagement")
	public void userManagement() {
		System.out.println("不倒翁");
	}
	
	@RequestMapping("/authority/users")
	public ModelAndView user(Integer pageNum,Integer drptId, Integer pageSize,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("/authority/users");
		List<DeptLevelDto> deptTree = sysTreeService.deptTree();
		List<Map<String, String>> selectAllTable = userServices.selectByPager(pageNum, pageSize,drptId,search);
		String jsonString = JSONObject.toJSONString(selectAllTable);
		//获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAllTable);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);		
		mv.addObject("deptTree", JSONObject.toJSONString(deptTree));
		return mv;
	}
	
	/**
	 * 分页查询用户
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/authority/users")
	@ResponseBody	
	public PageInfo<Map<String, String>> user(Integer pageNum, Integer pageSize,Integer drptId,Integer statusid  ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = userServices.selectByPager(pageNum, pageSize,drptId,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}
	/**
	 * 个人信息界面
	 * @return
	 */
	@RequestMapping("/authority/personalSettings")
	public ModelAndView personalSettings() {
		
		ModelAndView mv = new ModelAndView("/authority/personalSettings");
		Map<String, String> selectAllView = userMapper.selectAllView(LoginUtil.getUser().getuId());
		
		mv.addObject("userp", selectAllView);
		return mv;
	}
	/**
	 * 添加用户页数据
	 * @return 
	 */
	@PostMapping("/authority/addUserview")
	@ResponseBody
	public JSONObject addDeptview() {
		JSONObject jsonarray = new JSONObject();
		List<Map<String, String>> selectAllRoleView = roleServices.selectAllRoleView();
		jsonarray.put("deptAll", deptServices.findAllDept());
		jsonarray.put("roleAll", JSONObject.toJSONString(selectAllRoleView));
		return jsonarray;
	}
	
	/**
	 * 修改用户页数据
	 * @return 
	 */
	@PostMapping("/authority/updateUserview")
	@ResponseBody
	public JSONObject updateUserview(@RequestParam(name="ID",required=false) Integer uId) {
		Users user = userMapper.selectByPrimaryKey(uId);
		JSONObject jsonarray = new JSONObject();
		List<Map<String, String>> selectAllRoleView = roleServices.selectAllRoleView();
		List<Integer> list =new ArrayList<Integer>();
		List<Roleandusers> selectByUserId = roleandusersMapper.selectByUserId(uId);
		for (Roleandusers roleandusers : selectByUserId) {
			list.add(roleandusers.getRoleId());
		}
		jsonarray.put("roletoUser",JSONObject.toJSONString(list));
		jsonarray.put("roleAll", JSONObject.toJSONString(selectAllRoleView));
		jsonarray.put("deptAll", deptServices.findAllDept());
		jsonarray.put("user", user);
		return jsonarray;
	}
	/**
	 * 添加用户
	 * @return
	 */
	@PostMapping("/authority/addUser")
	@ResponseBody
	public String addUser(@RequestParam(name="parentid",required=false) Integer parentid,
			@RequestParam(name="uName",required=false) String uName,
			@RequestParam(name="uRole",required=false) Object uRole,
			@RequestParam(name="uEmail",required=false) String uEmail,
			@RequestParam(name="uIdcard",required=false) String uIdcard,
			@RequestParam(name="uPhone",required=false) String uPhone,
			@RequestParam(name="uSex",required=false) Integer uSex) {
			User user=new User();
			user.setUName(uName);
			user.setUSex(uSex);
			user.setUEmail(uEmail);
			user.setU_IDcard(uIdcard);
			user.setUPhone(uPhone);
			user.setuDept(parentid);
			String str= String.valueOf(uRole);
			@SuppressWarnings("unchecked")
			List<Integer> limap = JSONArray.parseObject(str,List.class);
		return userServices.save(user,limap)+"";
	}
	
	/**
	 * 修改用户信息
	 * @return
	 */
	@PostMapping("/authority/updateUser")
	@ResponseBody
	public String updateUser(@RequestParam(name="uId",required=false) Integer uId,
			@RequestParam(name="parentid",required=false) Integer parentid,
			@RequestParam(name="uName",required=false) String uName,
			@RequestParam(name="uEmail",required=false) String uEmail,
			@RequestParam(name="uRole",required=false) Object uRole,
			@RequestParam(name="uIdcard",required=false) String uIdcard,
			@RequestParam(name="uPhone",required=false) String uPhone,
			@RequestParam(name="uSex",required=false) Integer uSex) {
			Users user=new Users();
			user.setuId(uId);
			user.setuName(uName);
			user.setuSex(uSex);
			user.setuEmail(uEmail);
			user.setuIdcard(uIdcard);
			user.setuPhone(uPhone);
			user.setuDept(parentid);
			String str= String.valueOf(uRole);
			@SuppressWarnings("unchecked")
			List<Integer> limap = JSONArray.parseObject(str,List.class);
		return userServices.updateUser(user,limap)+"";
	}
	
	/**
	 * 修改用户个人信息
	 * @return
	 */
	@PostMapping("/authority/updateUserinfo")
	@ResponseBody
	public String updateUserinfo( 
			@RequestParam(name="headimg",required=false) String uHeadimg,
			@RequestParam(name="uName",required=false) String uName,
			@RequestParam(name="uEmail",required=false) String uEmail,
			@RequestParam(name="uIdcard",required=false) String uIdcard,
			@RequestParam(name="uPhone",required=false) String uPhone,
			@RequestParam(name="uSex",required=false) Integer uSex) {
			Users user=new Users();
			user.setuId(LoginUtil.getUser().getuId());
			user.setuName(uName);
			user.setuSex(uSex);
			user.setuEmail(uEmail);
			user.setuIdcard(uIdcard);
			user.setuPhone(uPhone);
			user.setuHeadimg(uHeadimg);
		return userServices.updateUserinfo(user)+"";
	}
	@RequestMapping("/authority/findByOne")
	@ResponseBody
	public JSONObject findByOne(Integer userId) {
		System.out.println("userId:"+userId);
		return userServices.findByone(userId);		
	}
	
	
	/**
	 * 修改用户密码
	 * @return
	 */
	@PostMapping("/authority/updatePwd")
	@ResponseBody
	public String updatePwd( 
			@RequestParam(name="pwd2",required=false) String pwd) {
			Users user=new Users();
			user.setuId(LoginUtil.getUser().getuId());
			user.setuPassword(pwd);
		return userServices.updateUserinfo(user)+"";
	}
}
